/*
 * ConsultaAgenda.java
 *
 * Created on 18/02/2011, 15:49:31
 */

package pbf.internalframe;

import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;
import org.hibernate.Session;
import pbf.conf.hibernate.HibernateUtil;
import pbf.conf.hibernate.entidades.Atendimento;
import pbf.dialog.DetalhesAtendimento;
import pbf.util.DataHora;

/**
 *
 * @author Ricardo
 */
public class ConsultarAtendimento extends javax.swing.JInternalFrame
{
    private static Object[] objetos = new Object[3];
    private static Session sessao;
    private static ArrayList<Atendimento> listaAtendimento = null;
    private DetalhesAtendimento detalhesAtendimento = new DetalhesAtendimento(null , true);

    public ConsultarAtendimento()
    {
        initComponents();
    }

    public void setAtiva()
    {
        sessao = HibernateUtil.openSession();
        campoBusca.setText("");
        setVisible(true);
         setTable();
    }

    private void setTable()
    {
        DefaultTableModel model = (DefaultTableModel) tabela.getModel();
        model.setNumRows(0);

        sessao.beginTransaction();

         switch(tipoBusca.getSelectedIndex()){
            case 0: listaAtendimento = (ArrayList<Atendimento>) sessao.createSQLQuery("SELECT ATENDIMENTO.* FROM familia,atendimento,agendamento WHERE FAMILIA_ATIVO='S' AND FAMILIA_ID=AGENDAMENTO_FK_FAMILIA AND AGENDAMENTO_ID=ATENDIMENTO_FK_AGENDAMENTO AND FAMILIA_NOME LIKE '"+campoBusca.getText().toUpperCase()+"%'").addEntity(Atendimento.class).list();break;
            case 1: listaAtendimento = (ArrayList<Atendimento>) sessao.createSQLQuery("SELECT ATENDIMENTO.* FROM familia,atendimento,agendamento WHERE FAMILIA_ATIVO='S' AND FAMILIA_ID=AGENDAMENTO_FK_FAMILIA AND AGENDAMENTO_ID=ATENDIMENTO_FK_AGENDAMENTO AND FAMILIA_CPF='"+campoBusca.getText().toUpperCase()+"' ").addEntity(Atendimento.class).list();break;
            case 2: listaAtendimento = (ArrayList<Atendimento>) sessao.createSQLQuery("SELECT ATENDIMENTO.* FROM familia,atendimento,agendamento WHERE FAMILIA_ATIVO='S' AND FAMILIA_ID=AGENDAMENTO_FK_FAMILIA AND AGENDAMENTO_ID=ATENDIMENTO_FK_AGENDAMENTO AND FAMILIA_NIS='"+campoBusca.getText().toUpperCase()+"' ").addEntity(Atendimento.class).list();break;
        }
        

        for(Atendimento f : listaAtendimento)
        {
            objetos[0] = DataHora.getData(f.getAtendimentoDtAtendimento());
            objetos[1] = f.getAgendamento().getFamilia().getFamiliaNome();
            objetos[2] = f.getStatusAtendimento().getStStatus();

            model.addRow(objetos);
        }
        sessao.getTransaction().commit();

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        campoBusca = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabela = new javax.swing.JTable();
        jSeparator1 = new javax.swing.JSeparator();
        jButton1 = new javax.swing.JButton();
        tipoBusca = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        setTitle("Sistema de Agendamento do Programa Bolsa Família");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameClosing(evt);
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
            }
        });
        getContentPane().setLayout(null);

        campoBusca.setFont(new java.awt.Font("Arial", 0, 16));
        getContentPane().add(campoBusca);
        campoBusca.setBounds(230, 40, 280, 30);

        tabela.setFont(new java.awt.Font("Tahoma", 0, 14));
        tabela.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"20/01/2011", "Ricardo Tavares", "CONCLUIDO"}
            },
            new String [] {
                "Dt. Atendimento", "Nome do Responsável", "Status"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabela.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabelaMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tabela);
        tabela.getColumnModel().getColumn(0).setPreferredWidth(80);
        tabela.getColumnModel().getColumn(1).setMinWidth(350);
        tabela.getColumnModel().getColumn(1).setPreferredWidth(300);
        tabela.getColumnModel().getColumn(1).setMaxWidth(200);
        tabela.getColumnModel().getColumn(2).setMinWidth(100);
        tabela.getColumnModel().getColumn(2).setPreferredWidth(100);
        tabela.getColumnModel().getColumn(2).setMaxWidth(100);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(10, 91, 570, 250);
        getContentPane().add(jSeparator1);
        jSeparator1.setBounds(0, 360, 610, 14);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pbf/imagens/magnifier-zoom.png"))); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);
        jButton1.setBounds(520, 40, 50, 30);

        tipoBusca.setFont(new java.awt.Font("Tahoma", 0, 14));
        tipoBusca.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Nome do Responsável", "CPF", "NIS" }));
        getContentPane().add(tipoBusca);
        tipoBusca.setBounds(30, 40, 190, 30);

        jLabel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Selecione o tipo da busca e clique no botão", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 14))); // NOI18N
        getContentPane().add(jLabel2);
        jLabel2.setBounds(10, 10, 570, 70);

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pbf/imagens/cross-circle.png"))); // NOI18N
        jButton2.setText("Fechar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2);
        jButton2.setBounds(473, 370, 100, 30);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-606)/2, (screenSize.height-446)/2, 606, 446);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    setTable();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    setVisible(false);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void tabelaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelaMouseClicked
        if (evt.getClickCount() == 2)
        {
                int linha = tabela.getSelectedRow();
                detalhesAtendimento.setFormulario(listaAtendimento.get(linha), listaAtendimento.get(linha).getAgendamento().getDescricaoDoAtendimento(), listaAtendimento.get(linha).getStatusAtendimento());
        }
    }//GEN-LAST:event_tabelaMouseClicked

    private void formInternalFrameClosing(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosing
    sessao.close();
    }//GEN-LAST:event_formInternalFrameClosing


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private static javax.swing.JTextField campoBusca;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    public static javax.swing.JTable tabela;
    private static javax.swing.JComboBox tipoBusca;
    // End of variables declaration//GEN-END:variables
}
